Useful Python by Langridge Stuart;
Author:Langridge, Stuart; [Stuart Langridge]
Language: eng
Format: epub
ISBN: 978-1-925836-57-8
Publisher: SitePoint
Published: 2022-04-06T23:00:00+00:00
For this, weâll need to read the metadata from Python, and the best way to do any image manipulation is with Pillow, the Python Imaging Library. The metadata we want here is called IPTC, defined in extreme detail by the International Press Telecommunications Council in the IPTC Photo Metadata Standard. In particular, we want the imageâs title tag, with code 2:120. We can define a convenient variable for that:
IPTC_Title = (2, 120)
Next, letâs loop over each of our images. Pythonâs glob module is useful for this. A glob (a term from Unix prehistory, short for âglobalâ) is a pattern using a wildcard character and file specification to gather a list of all files matching that pattern. (For example, *.txt will match a list of files such as myfile.txt, 1.txt, everything.txt, and so on.) In Python, glob.glob("*.txt") does the same thing. It returns a list of matching filenames, which we can then iterate over. We can also open each one with the Python Imaging Library:
import glob, os from PIL import Image for image_filename in glob.glob("*.jpg"): im = Image.open(image_filename)
Here, we have a reference to each image. Pillow can do all sorts of image manipulations, such as resizing, changing the content, saving in different formats, and so on. But for now, all we want to do is read the metadata. For that, we use Pillowâs IptcImagePlugin, and we can have it print out the relevant image title:
from PIL import Image, IptcImagePlugin for image_filename in glob.glob("*.jpg"): im = Image.open(image_filename) iptc = IptcImagePlugin.getiptcinfo(im) im.close() title = iptc[IPTC_Title].decode("utf-8")
Now that we know the title for a given image, we can use it to construct a new filename, and then use os.rename to rename the file to this new name. The os.rename function does more than simply renaming; it can also move a file to a different directory or different disk. But in this case, weâre only renaming the file in place:
destination_filename = f"{title}.jpg" print(f"Renaming {image_filename} -> {destination_filename}") if os.path.exists(destination_filename): print(f"{destination_filename} already exists; skipping") else: os.rename(image_filename, destination_filename)
Weâre being a little careful here. If the destination_filename already exists, then os.rename canâdepending on our OSâeither overwrite it without warning or throw an exception. So we check beforehand whether that file exists, and if it does, we assume that weâve already handled this image and therefore skip it.
Now, a list of the directory shows the files all renamed according to their titles. We could look in the file manager for this, but weâre in Python, so letâs use Python for it:
>>> import os >>> os.listdir() ['avocado.jpg', 'kiwifruit.jpg', 'lemon.jpg', 'apple.jpg', 'strawberry.jpg', 'rename-images.py', 'orange.jpg']
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(12520)
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Dependency Injection in .NET by Mark Seemann(9293)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6743)
Microservices with Go by Alexander Shuiskov(6510)
Practical Design Patterns for Java Developers by Miroslav Wengner(6408)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6386)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Angular Projects - Third Edition by Aristeidis Bampakos(5765)
The Art of Crafting User Stories by The Art of Crafting User Stories(5296)
NetSuite for Consultants - Second Edition by Peter Ries(5241)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5058)
Kotlin in Action by Dmitry Jemerov(5019)
